/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import constant.CourseDB;
import constant.CourseTypeEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author 乐山
 */
public class Grades {

    /**
     * 基础选修
     */
    private List<CourseGrade> basicEC = new ArrayList<>();
    /**
     * 专业必修
     */
    private List<CourseGrade> specialRC = new ArrayList<>();
    /**
     * 专业选修
     */
    private List<CourseGrade> specialEC = new ArrayList<>();

    public List<CourseGrade> getBasicEC() {
        return basicEC;
    }

    public void setBasicEC(List<CourseGrade> basicEC) {
        this.basicEC = basicEC;
    }

    public List<CourseGrade> getSpecialRC() {
        return specialRC;
    }

    public void setSpecialRC(List<CourseGrade> specialRC) {
        this.specialRC = specialRC;
    }

    public List<CourseGrade> getSpecialEC() {
        return specialEC;
    }

    public void setSpecialEC(List<CourseGrade> specialEC) {
        this.specialEC = specialEC;
    }

    public Double calcBasicECCredit() {
        double sumCredit = 0.0;
        for (CourseGrade e : basicEC) {
            if (e.getFinalGrade() >= 60) {
                sumCredit += e.getCredit();
            }
        }
        return sumCredit;
    }

    public Double calcSpecialECCredit() {
        double sumCredit = 0.0;
        for (CourseGrade e : specialEC) {
            if (e.getFinalGrade() >= 60) {
                sumCredit += e.getCredit();
            }
        }
        return sumCredit;
    }

    public Double calcSpeicalRCCredit() {
        double sumCredit = 0.0;
        for (CourseGrade e : specialRC) {
            if (e.getFinalGrade() >= 60) {
                //System.out.println(e.getName()+" "+e.getCredit());
                sumCredit += e.getCredit();
            }
        }
        return sumCredit;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("\n基础选修:\n");
        for (CourseGrade e : basicEC) {
            sb.append(e);
        }
        sb.append("\n专业选修:\n");
        for (CourseGrade e : specialEC) {
            sb.append(e);
        }
        sb.append("\n专业必修:\n");
        specialRC.forEach(e -> {
            sb.append(e);
        });
        return sb.toString();
    }
    
    /**
     * 判断某个修习类别是否通过
     * @param cte
     * @return 
     */
    public Boolean isPassed(CourseTypeEnum cte) {
        int result = 0;
        switch (cte) {
            case BASICEC:
                result = Double.compare(calcBasicECCredit(), cte.getCredit());
                break;
            case SPECIALEC:
                result = Double.compare(calcSpecialECCredit(), cte.getCredit());
                break;
            case SPECIALRC:
                result = Double.compare(calcSpeicalRCCredit(), cte.getCredit());
                break;
            default:
                throw new AssertionError();
        }
        return result >= 0;
    }
    
    /**
     * 判断所有修习类别是否通过
     * @return 
     */
    public Boolean isAllPassed() {
        return isPassed(CourseTypeEnum.BASICEC)
                && isPassed(CourseTypeEnum.SPECIALEC)
                && isPassed(CourseTypeEnum.SPECIALRC);
    }
    
    /**
     * 获取已通过的基础选修课程
     * @return 
     */
    public Set<String> getPassedCoursesName(CourseTypeEnum cte){
        Set<String> r = new HashSet<>();
        List<CourseGrade> courseList = null;
        switch (cte) {
            case BASICEC:
                courseList = basicEC;
                break;
            case SPECIALEC:
                courseList = specialEC;
                break;
            case SPECIALRC:
                courseList = specialRC;
                break;
            default:
                throw new AssertionError();
        }
        for (CourseGrade e : courseList) {
            if (e.getFinalGrade()>=60)
                r.add(e.getName());
        }
        return r;
    }
    

    public Map<String, Double> getFailedBasicEC() {
        Set<String> allBECSet = CourseDB.getBasicECMap().keySet();
        allBECSet.removeAll(getPassedCoursesName(CourseTypeEnum.BASICEC));
        Map<String, Double> result = new HashMap<>();
        for (String e : allBECSet) {
            result.put(e, CourseDB.getBasicECMap().get(e));
        }
        return result;
    }

    public Map<String, Double> getFailedSpecialEC() {
        Set<String> allSECSet = CourseDB.getSpecialECMap().keySet();
        allSECSet.removeAll(getPassedCoursesName(CourseTypeEnum.SPECIALEC));
        Map<String, Double> result = new HashMap<>();
        for (String e : allSECSet) {
            result.put(e, CourseDB.getSpecialECMap().get(e));
        }
        return result;
    }

    public Map<String, Double> getFailedSpecialRC() {
        Set<String> allSRCSet = CourseDB.getSpecialRCMap().keySet();
        System.out.println(getPassedCoursesName(CourseTypeEnum.SPECIALRC));
        allSRCSet.removeAll(getPassedCoursesName(CourseTypeEnum.SPECIALRC));
        Map<String, Double> result = new HashMap<>();
        for (String e : allSRCSet) {
            result.put(e, CourseDB.getSpecialRCMap().get(e));
        }
        return result;
    }

}
